<template>
  <div class="cart">
    <HeadView>
      <span slot="center">购物车</span>
      <span slot="right"><i class="el-icon-search"></i></span>
    </HeadView>
    <!-- 判断 -->
    <div class="cart-main" v-if="islogin != null">
      <div class="cartlist" v-for="item in cartdata" :key="item.id">
        <div>
          <img :src="require('@/assets/img/' + item.img)" />
        </div>
        <div>
          <p>{{ item.title }}</p>
          <p>{{ item.pro }}</p>
          <p class="price">￥{{ item.price * item.count }}</p>
        </div>
        <div class="cart-s">
          <button class="right" @click="item.count > 1 ? item.count-- : ''">
            -
          </button>
          <span>{{ item.count }}</span>
          <button class="left" @click="item.count++">+</button>
        </div>
      </div>
    </div>
    <div v-else>
      <button class="login" @click="log">请登录</button>
    </div>
    <!-- 底部订单部分 -->
    <div class="bottom">
      <p>
        <input type="checkbox" v-model="istrue" @change="!istrue" />
        <span>全选</span>
      </p>
      <p>
        <span>合计：￥</span>
        <span class="red">{{ allcheck() }}</span>
      </p>
      <button class="sub">提交</button>
    </div>
    <nav-view></nav-view>
  </div>
</template>
<script>
import HeadView from "@/components/HeadView.vue";
import NavView from "../components/NavView.vue";

export default {
  components: { NavView, HeadView },
  data() {
    return {
      islogin: localStorage.getItem("tel"),
      istrue: true,
    };
  },
  methods: {
    log() {
      this.$router.push("/");
    },
    // 全选
    allcheck() {
      let sum = 0;
      if (this.istrue) {
        this.cartdata.forEach((item) => {
          sum += item.price * item.count;
        });
      }
      return sum;
    },
  },
  computed: {
    cartdata() {
      // 将购物车的数据return出去
      return this.$store.state.cartList;
    },
  },
};
</script>
<style lang="scss" scoped>
.cart {
  height: 100%;
  position: relative;
}
.login {
  width: 100%;
  border: 0;
  height: 50px;
  background: #00c562;
  color: #fff;
  font-size: 18px;
}
.cart-main {
  margin-top: 10px;
  display: flex;
  flex-direction: column;
  height: 800px;
  .cartlist {
    margin-bottom: 10px;
    height: 126px;
    background: #fff;
    display: flex;
    div {
      flex: 1;
      img {
        height: 100%;
      }
      p {
        margin-top: 10px;
      }
      p:nth-child(2) {
        font-size: 13px;
        color: rgb(37, 35, 35);
      }
    }
    div:nth-child(2) {
      flex: 2;
      .price {
        margin-top: 25px;
      }
    }
  }
}
.cart-s {
  margin-top: 80px;
  span {
    margin: 0 10px;
  }
}
.right,
.left {
  width: 24px;
  height: 24px;
  border: 1px solid red;
  border-radius: 12px;
  font-size: 20px;
  background: transparent;
  color: red;
}
.bottom {
  position: fixed;
  bottom: 64px;
  // bottom: 0;
  left: 0;
  width: 100%;
  height: 60px;
  background: #fff;
  display: flex;
  justify-content: space-around;
  align-items: center;
  .sub {
    width: 117px;
    height: 38px;
    background: red;
    color: #fff;
    line-height: 38px;
    border-radius: 18px;
    border: 0;
  }
  p {
    flex: 1;
    text-align: center;
    &:nth-child(2) {
      flex: 2;
      display: flex;
      span {
        flex: 1;
        text-align: right;
      }
      .red {
        color: red;
        text-align: left;
        // flex: 2;
      }
    }
    input {
      width: 15px;
      height: 15px;
    }
  }
}
</style>
